package net;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import net.schmizz.sshj.connection.channel.direct.Session.Command;
import core.models.Rule;

public class SSHConnectionProxy implements SSHConnection {
	private static SSHConnectionImpl connection;
	
	public SSHConnectionProxy() { }
	
	public SSHConnectionProxy(String username, char[] password, String hostname, int port) throws IOException {
		setUp(username, password, hostname, port);
	}
	
	public static void setUp(String username, char[] password, String hostname, int port) throws IOException {
		if (isConnected()) {
			return;
		}
		connection = new SSHConnectionImpl(username, password, hostname, port);
	}
	
	public static void close() throws IOException {
		if (isConnected()) {
			connection.disconnect();
			connection = null;
		}
	}
	
	public static boolean isConnected() {
		return connection != null && connection.isConnected();
	}
	
	public void reconnect() throws IOException {
		if (connection != null)
			connection = new SSHConnectionImpl(connection.getUsername(),
					connection.getPassword(), connection.getHostname(),
					connection.getPort());
	}

	@Override
	public Command execute(String command) throws IOException {
		return connection.execute(command);
	}

	@Override
	public File downloadInterfaces() throws IOException {
		return connection.downloadInterfaces();		
	}

	@Override
	public void sendInterfaces(File file) throws IOException {
		connection.sendInterfaces(file);		
	}

	@Override
	public List<String> getRules(String table) throws IOException {
		return connection.getRules(table);
	}

	@Override
	public List<String> getInterfacesNames() throws IOException {
		return connection.getInterfacesNames();
	}

	@Override
	public void sendRule(Rule rule) throws IOException {
		connection.sendRule(rule);
	}

	@Override
	public File downloadDhcpConf() throws IOException {
		return connection.downloadDhcpConf();
	}

	@Override
	public void sendDhcpdConf(File file) throws IOException {
		connection.sendDhcpdConf(file);
	}

	@Override
	public long getReceivedPackets(String ifaceName) throws IOException {
		return connection.getReceivedPackets(ifaceName);
	}

	@Override
	public long getReceivedBytes(String ifaceName) throws IOException {
		return connection.getReceivedBytes(ifaceName);
	}

	@Override
	public long getSentPackets(String ifaceName) throws IOException {
		return connection.getSentPackets(ifaceName);
	}

	@Override
	public long getSentBytes(String ifaceName) throws IOException {
		return connection.getSentBytes(ifaceName);
	}

	@Override
	public Collection<String> getIptablesLog(String filter) throws IOException {
		return connection.getIptablesLog(filter);
	}

}
